package com.payment.ktb.activity.main4;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.adapter.AdapterItem1Heigth40TextSize15;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.payment.ktb.R.id.lv_branchbankprovince;

/**
 * Created by admin on 2017/9/14.
 */

public class BranchBankCityActivity extends BaseActivity {


    @BindView(R.id.lv_branchbankcity)
    ListView lv_branchbankcity;

    List<String> listDatas;
    AdapterItem1Heigth40TextSize15 adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_branchbankcity);
        ButterKnife.bind(this);
        initTitle("选择支行");
        initData();

    }

    private void initData() {

        getCities();
        lv_branchbankcity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(mContext,SettleBanknameActivity.class);
                intent.putExtra("bankType",getIntent().getStringExtra("bankType"));
                intent.putExtra("bankSimpleName",getIntent().getStringExtra("bankSimpleName"));
                intent.putExtra("cardNo",getIntent().getStringExtra("cardNo"));
                intent.putExtra("bankphone",getIntent().getStringExtra("bankphone"));
                intent.putExtra("city",listDatas.get(i));
                intent.putExtra("httpActionType",getIntent().getStringExtra("httpActionType"));
                startActivity(intent);
                finish();
            }
        });

    }

    public void getCities() {

        progressDialogUtils.showProgress();

        //构造传输参数
        Map<String, String> map = new HashMap<String,String>();
        map.put("province",getIntent().getStringExtra("province"));

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.CardAreaCitySearch, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    //data
                    listDatas = new Gson().fromJson(jsonObject.getJSONArray("city").toString(), new TypeToken<List<String>>() {}.getType());
                    //adapter
                    adapter = new AdapterItem1Heigth40TextSize15(mContext,listDatas);
                    //bind
                    lv_branchbankcity.setAdapter(adapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });
    }
}
